/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.manager.deleteflights;

import assignment_cp3120.dataObjects.AllFlights;
import assignment_cp3120.dataObjects.Flight;
import assignment_cp3120.ucm.UC2AL;
import assignment_cp3120.ucm.UCDeleteFlight;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class DeleteFlightsView extends JPanel {
        private JComboBox currentFlightsCBox;
        private JButton deleteFlightBtn;
        private JComponent deleteFlightsPanel;
        private DeleteFlightsModel model;
        private final int DESCIPTION_LENGTH = 2;
        
    public DeleteFlightsView() {
        model = new DeleteFlightsModel();
        createPanel();
    }
    private void createPanel(){
        deleteFlightBtn = new JButton("Delete");
        deleteFlightBtn.addActionListener(new UC2AL((new UCDeleteFlight(this,model))));
        deleteFlightBtn.setEnabled(false);
        
        String[] currentFlights = new String[AllFlights.getInstance().size()+DESCIPTION_LENGTH];

        DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
        currentFlights[0] = "Select a flight to delete from the list:";
        currentFlights[1] = "----------------------------------------";
        for (int i = 0; i < AllFlights.getInstance().size(); i++) {
            currentFlights[i+DESCIPTION_LENGTH] = AllFlights.getInstance().get(i).toString();
        }
        
        deleteFlightsPanel = new JPanel();
        currentFlightsCBox = new JComboBox(currentFlights);
        currentFlightsCBox.addItemListener(new EditCBoxChange());
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        buttonPanel.add(deleteFlightBtn);
        
        deleteFlightsPanel.add(buttonPanel);
        
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        JPanel currentFlightsPanel = new JPanel(new FlowLayout());
        currentFlightsPanel.add(currentFlightsCBox);
        
        add(new JPanel());
        add(currentFlightsPanel);
        add(deleteFlightsPanel);
    }
    public Flight getDeleteFlight () {
        return AllFlights.getInstance().get(currentFlightsCBox.getSelectedIndex()-DESCIPTION_LENGTH);
    }
    public void clearView() {
        this.removeAll();
        createPanel();
        this.validate();
    }
    private class EditCBoxChange implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            JComboBox tempCBox = (JComboBox)e.getSource();

            if (tempCBox.getSelectedIndex() > DESCIPTION_LENGTH-1) {
                deleteFlightBtn.setEnabled(true);
            }
            else {
                deleteFlightBtn.setEnabled(false);
            }
        }
    }
}
